﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class InventoryTracker_DropD : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {

            // This is because Table[1] contains Categories

            GridView1.DataSource = GetDataSet().Tables[1];

            GridView1.DataBind();

        }

    }   

    private DataSet GetDataSet()
    {
//        string query = @"SELECT ItemCode,ItemName FROM MaterialMaster m
//            Select ItemDescription FROM MaterialMaster n;
        string query = @"SELECT p.CategoryID,p.ProductID, p.ProductName FROM Products p

               SELECT c.CategoryID,c.CategoryName FROM Categories c";
        string connectionString = @"Server=.\SQLEXPRESS;Database=F:\inv\App_Data\TimeTracker.mdf;;Trusted_Connection=true";
         
        SqlConnection myConnection = new SqlConnection(connectionString);

        SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);

        DataSet ds = new DataSet();

        ad.Fill(ds);

        return ds;

    }


    protected  void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

DataTable myTable = new DataTable();

DataColumn productIDColumn = new DataColumn("ProductID");

DataColumn productNameColumn = new DataColumn("ProductName");

myTable.Columns.Add(productIDColumn);

myTable.Columns.Add(productNameColumn);

DataSet ds = new DataSet();

ds = GetDataSet();

int categoryID = 0;

string expression = String.Empty;

if (e.Row.RowType == DataControlRowType.DataRow)

{

categoryID = Int32.Parse(e.Row.Cells[0].Text);

expression = "CategoryID = " + categoryID;

DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");

DataRow[] rows = ds.Tables[0].Select(expression);

 

foreach (DataRow row in rows)

{

DataRow newRow = myTable.NewRow();

newRow["ProductID"] = row["ProductID"];

newRow["ProductName"] = row["ProductName"];

myTable.Rows.Add(newRow);




}

ddl.DataSource = myTable;

ddl.DataTextField = "ProductName";

ddl.DataValueField = "ProductID";

ddl.DataBind();

}

}
}
